package com.boot.project.service.impl;

import com.boot.project.dao.CourtDao;
import com.boot.project.dao.CourtTimeDao;
import com.boot.project.domain.CourtDO;
import com.boot.project.domain.CourtTimeDO;
import com.boot.project.service.CourtService;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.ImageProducer;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourtServiceImpl implements CourtService {
    @Autowired
    private CourtDao courtDao;
    @Autowired
    private CourtTimeDao courtTimeDao;

    @Override
    public List<CourtDO> getCourt(String dateTime) {
        return courtDao.getCourt(dateTime);
    }

    @Override
    public List<CourtTimeDO> getCourtTime(Long courtId) {
        /** 因为比如现在是11点，不管11以前的订场时间段是否有被订，都不用显示,
         * 所以要获取当前时间是几点，来判断能获取的时间段*/
        Date time = new Date();
        Integer hours = time.getHours();  // 获取当前的时间的小时数
        Integer minutes = time.getMinutes(); // 获取当前的时间的分钟数
        Double hourMin = Double.valueOf(hours.toString()+"."+minutes.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("courtId", courtId);
        map.put("hours", hourMin);
        return courtTimeDao.getCourtTime(map);
    }

    @Override
    public Map<String, Object> saveCourt(CourtDO courtDO) {
        Map<String, Object> mapResult = new HashMap<>();
        mapResult.put("msg", "ok");
        mapResult.put("code", 200);
        Integer saveFlag = courtDao.saveCourt(courtDO);
        if (saveFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> saveCourtTime(CourtTimeDO courtTimeDO) {
        Map<String, Object> mapResult = new HashMap<>();
        /** 时间段状态0是可订时间段，1为不可订时间段 */
        // 因为是新增时间段，所以要将状态设置为0
        courtTimeDO.setStatus(0);
        Integer saveFlag = courtTimeDao.saveCourtTime(courtTimeDO);
        if (saveFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> updateCourt(CourtDO courtDO) {
        Map<String, Object> mapResult = new HashMap<>();
        Integer updateFlag = courtDao.updateCourt(courtDO);
        if (updateFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> updateCourtTime(CourtTimeDO courtTimeDO) {
        Map<String, Object> mapResult = new HashMap<>();
        Integer updateFlag = courtTimeDao.updateCourtTime(courtTimeDO);
        if (updateFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> deleteCourt(Long id) {
        Map<String, Object> mapResult = new HashMap<>();
        // 删除球场
        Integer deleteCourtFlag = courtDao.deleteCourt(id);
        // 球场删除之后，球场下面的时间段也要被删除
        Integer deleteCourtTimeFlag = courtTimeDao.listDelete(id);
        if (deleteCourtFlag >= 0 && deleteCourtTimeFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> deleteCourtTime(Long id) {
        Map<String, Object> mapResult = new HashMap<>();
        Integer deleteFlag = courtTimeDao.deleteCourtTime(id);
        if (deleteFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> changeStatus(Long courtTimeId) {
        Map<String, Object> mapResult = new HashMap<>();
        Integer changeFlag = courtTimeDao.changeStatus(courtTimeId);
        if (changeFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

    @Override
    public Map<String, Object> getStatus(Long courtTimeId) {
        Map<String, Object> result = new HashMap<>();
        result.put("status", courtTimeDao.getStatus(courtTimeId));
        return result;
    }

    @Override
    public Integer batchSave(List<CourtDO> courtDOS) {
        return courtDao.batchSave(courtDOS);
    }

    @Override
    public List<CourtTimeDO> getBatchCourtTime(Long courtId) {
        return courtTimeDao.getBatchCourtTime(courtId);
    }

    @Override
    public Integer updateCourtDate(CourtDO courtDO) {
        return courtDao.updateCourtDate(courtDO);
    }

    @Override
    public Integer updateCourtTimeStatus(Long courtId) {
        return courtTimeDao.updateCourtTimeStatus(courtId);
    }

    @Override
    public Map<String, Object> changeStatusNormal(Long courtTimeId) {
        Map<String, Object> mapResult = new HashMap<>();
        Integer changeFlag = courtTimeDao.changeStatusNormal(courtTimeId);
        if (changeFlag >= 0) {
            mapResult.put("msg", "ok");
            mapResult.put("code", 200);
            return mapResult;
        } else {
            mapResult.put("msg", "error");
            mapResult.put("code", 500);
            return mapResult;
        }
    }

}
